package com.java.tutorials.solr;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by xuzhike on 2015/12/28.
 */
public class SolrDemo {
//        public static final String SOLR_URL = "http://192.168.31.145:8080/solr/core3";
//    public static final String SOLR_URL = "http://192.168.31.145:8983/solr/core1";
    public static final String SOLR_URL = "http://localhost:8983/solr/core2";

//    public static final String SOLR_URL = "http://localhost:8080/solr/core521";

    public static void main(String[] args) throws IOException, SolrServerException {
//        addDocs();
//    queryDocs();

//        addUserInfoList();
//            queryUserInfoList();
//        deleteAllUserList();

        int t = Runtime.getRuntime().availableProcessors();
        System.out.println("i="+t);
    }



    private static void queryDocs() throws SolrServerException, IOException {
        HttpSolrServer server = new HttpSolrServer(SOLR_URL);
        ModifiableSolrParams params = new ModifiableSolrParams();

        // 查询关键词，*:*代表所有属性、所有值，即所有index

        params.set("q", "*:*");
        // 分页，start=0就是从0开始，，rows=5当前返回5条记录，第二页就是变化start这个值为5就可以了。
        params.set("start", 0);
        params.set("rows", Integer.MAX_VALUE);

        QueryResponse response = server.query(params);

        // 搜索得到的结果数
        System.out.println("Find:" + response.getResults().getNumFound());
    }

    private static void queryUserInfoList() throws IOException, SolrServerException {
        String SOLR_URL = "http://localhost:8983/solr/complex_object";
        SolrClient client = new HttpSolrClient(SOLR_URL);
        ModifiableSolrParams params = new ModifiableSolrParams();

        // 查询关键词，*:*代表所有属性、所有值，即所有index

        params.set("q", "*:*");
        // 分页，start=0就是从0开始，，rows=5当前返回5条记录，第二页就是变化start这个值为5就可以了。
        params.set("start", 0);
        params.set("rows", Integer.MAX_VALUE);
        QueryResponse response = client.query(params);
        SolrDocumentList list = response.getResults();
        DocumentObjectBinder binder = new DocumentObjectBinder();
        List<UserInfo> beanList=binder.getBeans(UserInfo.class, list);
        for(UserInfo news:beanList){
            System.out.println(news);
        }
    }
    private static void addUserInfoList() throws IOException, SolrServerException {
        String SOLR_URL = "http://localhost:8983/solr/complex_object";
        List<UserInfo> userInfoList = new ArrayList<UserInfo>();
        Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setId(""+System.nanoTime());
        userInfo1.setName("小明");
        userInfo1.setAge(10);
        userInfo1.setMan(true);
        userInfo1.getPhones().add("11111111");
        userInfo1.getPhones().add("222222222");
        Address addr1 = new Address();
        addr1.setAddressName("小明地址---1");
        addr1.setNumber(10002);

        Address addr2 = new Address();
        addr2.setAddressName("小明地址---2");
        addr2.setNumber(7002);
        userInfo1.getAddressList().add(addr1);
        userInfo1.getAddressList().add(addr2);
        userInfo1.updateAddrList();


        SolrInputDocument document1 = new SolrInputDocument();
        document1.addField("user",userInfo1);
        document1.addField("id",""+System.nanoTime());
        docs.add(document1);

        UserInfo userInfo2 = new UserInfo();
        userInfo2.setId(""+System.nanoTime());
        userInfo2.setName("徐志科");
        userInfo2.setAge(40);
        userInfo2.setMan(true);
        Address addr12 = new Address();
        addr12.setAddressName("珠海蔚蓝堡");
        addr12.setNumber(3762);

        Address addr22 = new Address();
        addr22.setAddressName("梅州大埔");
        addr22.setNumber(4873);
        userInfo2.getAddressList().add(addr12);
        userInfo2.getAddressList().add(addr22);
        userInfo2.updateAddrList();

        userInfoList.add(userInfo1);
        userInfoList.add(userInfo2);
        SolrClient client = new HttpSolrClient(SOLR_URL);
        client.addBeans(userInfoList);
        client.commit();
//        HttpSolrServer server = new HttpSolrServer(SOLR_URL);
//        server.addBeans(userInfoList);
//        server.add(docs.iterator());
    }

    private static void deleteAllUserList() throws IOException, SolrServerException {
        String SOLR_URL = "http://localhost:8983/solr/complex_object";
        HttpSolrServer server = new HttpSolrServer(SOLR_URL);
        server.deleteByQuery("*:*");
        server.commit();
    }

    private static void addDocs() throws IOException, SolrServerException {
        String[] words = { "中央全面深化改革领导小组", "第四次会议", "审议了国企薪酬制度改革", "考试招生制度改革",
                "传统媒体与新媒体融合等", "相关内容文件", "习近平强调要", "逐步规范国有企业收入分配秩序",
                "实现薪酬水平适当", "结构合理、管理规范、监督有效", "对不合理的偏高", "过高收入进行调整",
                "深化考试招生制度改革", "总的目标是形成分类考试", "综合评价", "多元录取的考试招生模式", "健全促进公平",
                "科学选才", "监督有力的体制机制", "着力打造一批形态多样", "手段先进", "具有竞争力的新型主流媒体",
                "建成几家拥有强大实力和传播力", "公信力", "影响力的新型媒体集团" };
        Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
        for (int i = 1; i < 100; i++) {
            SolrInputDocument doc1 = new SolrInputDocument();
            doc1.addField("id", "id" + i, 1.0f);
            doc1.addField("name", words[i % 21], 1.0f);
            doc1.addField("price", 10 * i);
            docs.add(doc1);
        }
        HttpSolrServer server = new HttpSolrServer(SOLR_URL);
        server.add(docs.iterator());
    }
}
